package view.ui 
{
	import comp.display.Label;
	import event.ShowPanelEvent;
	//import comp.display.Window;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import model.data.NiaoyuEntry;
	import model.data.NiaoyuIdea;
	
	/**
	 * 列表中单个词条的单元
	 * @author Qi Donghui
	 */
	public class EntryItem extends Sprite 
	{
		public var _bg:Sprite;
		
		private var _data:NiaoyuEntry;
		
		private var _selected:Boolean;
		
		private var _ideaItem:IdeaItem;
		
		private var _theight:Number;
		
		///收藏按钮
		private var _btnAddIdea:SKbtnAddIdea;
		///转发按钮
		private var _btnRepost:SKbtnRepost;
		///收藏按钮
		private var _btnFav:SKbtnFav;
		///贡献者姓名展示
		private var _tProvider:Label;
		///展开详情按钮
		private var _btnExpand:MovieClip;
		
		public function EntryItem(data:NiaoyuEntry) 
		{
			_data = data;
		}
		
		private function addRollOverEffect():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER, onOverThis);
			this.addEventListener(MouseEvent.ROLL_OUT, onOutThis);
		}
		
		private function removeRollOverEffect():void
		{
			
		}
		
		////////////////////////////////////////////////////////////
		//// 共有函数
		////////////////////////////////////////////////////////////
		public function render():void
		{
			if (_bg == null) _bg = new Sprite();
			addChild(_bg);
			
			var t1:Label = new Label(this, 20, 15, _data.word, new TextFormat("Arial", 18, null, true));
			var t2:Label = new Label(this, 20, 37, _data.explain, new TextFormat(null, 12, 0x4c4c4c));
			if (_data.arrIdeas.length == 0) {
				var t3:Label = new Label(this, 20, this.height + 20, "目前没有创意，还不抢占沙发！", new TextFormat(null, 12, 0xbfbfbf));
			}else {
				var idea:NiaoyuIdea = _data.arrIdeas[0];
				_ideaItem = new IdeaItem(_data.arrIdeas[0]);
				_ideaItem.addEventListener(Event.CHANGE, onItemChange);
				_ideaItem.x = 20;
				_ideaItem.y = 60;
				addChild(_ideaItem);
				_ideaItem.render();
				
				_btnExpand = new SKbtnExpand();
				_btnExpand.buttonMode = true;
				_btnExpand.x = _ideaItem.x + 436;
				_btnExpand.y = _ideaItem.y + 14;
				_btnExpand.addEventListener(MouseEvent.CLICK, onExpandBtnClick);
				addChild(_btnExpand);
				
				_btnRepost = new SKbtnRepost();
				_btnFav = new SKbtnFav();
				_btnFav.x = 450;
				_btnRepost.x = 355;		
				addChild(_btnFav);
				addChild(_btnRepost);
				
				_tProvider = new Label(this, 20, this.height + 20, "创意贡献by" + idea.name, new TextFormat(null, 12, 0xbfbfbf));
			}
			
			_btnAddIdea = new SKbtnAddIdea();
			_btnAddIdea.x = 515;
			addChild(_btnAddIdea);
			
			_btnAddIdea.addEventListener(MouseEvent.CLICK, onBtnAddIdeaClick);
			addRollOverEffect();
			
			reRender();
		}
		
		private function reRender():void
		{
			if (_data.arrIdeas.length > 0) _theight = this.height + 20;
			else _theight = this.height - 15;
			if (_tProvider != null) _tProvider.y = this.height + 20;	
			_bg.graphics.clear();
			_bg.graphics.beginFill(0xfffded, 0);
			_bg.graphics.drawRect(0, 0, 600, _theight + 30);
			_bg.graphics.endFill();	
			_bg.graphics.lineStyle(1, 0xebebeb);
			_bg.graphics.moveTo(0, 0);
			_bg.graphics.lineTo(600, 0);
			_bg.graphics.moveTo(0, _theight + 30);
			_bg.graphics.lineTo(600, _theight + 30);
			
			_btnAddIdea.y = _theight;
			if (_btnRepost != null) _btnRepost.y = _btnFav.y = _theight;
		}
		
		public function destroy():void
		{
			
		}
		
		////////////////////////////////////////////////////////////
		//// 事件处理
		////////////////////////////////////////////////////////////
		private function onOutThis(e:MouseEvent):void 
		{
			_bg.graphics.clear();
			_bg.graphics.beginFill(0xfffded, 0);
			_bg.graphics.drawRect(0, 0, 600, _theight + 30);
			_bg.graphics.endFill();				
			_bg.graphics.lineStyle(1, 0xebebeb);
			_bg.graphics.moveTo(0, 0);
			_bg.graphics.lineTo(600, 0);
			_bg.graphics.moveTo(0, _theight + 30);
			_bg.graphics.lineTo(600, _theight + 30);
		}
		
		private function onOverThis(e:MouseEvent):void 
		{
			_bg.graphics.clear();	
			_bg.graphics.beginFill(0xfffded);
			_bg.graphics.drawRect(0, 0, 600, _theight + 30);
			_bg.graphics.endFill();			
			_bg.graphics.lineStyle(1, 0xf4edb2);
			_bg.graphics.moveTo(0, 0);
			_bg.graphics.lineTo(600, 0);
			_bg.graphics.moveTo(0, _theight + 30);
			_bg.graphics.lineTo(600, _theight + 30);
			
			this.parent.addChild(this);
		}		
		
		private function onBtnAddIdeaClick(e:MouseEvent):void 
		{
			//var w:Window = new Window(this.stage, stage.stageWidth * 0.5 - 100, stage.stageHeight * 0.5 - 100, "添加创意：");
			//w.centerWindow();
			//w.setSize(405, 509);
			//w.centerWindow();
			dispatchEvent(new ShowPanelEvent(ShowPanelEvent.SHOW_ADD_IDEA_PANEL));
		}	
		
		private function onItemChange(e:Event):void 
		{
			reRender();
			dispatchEvent(e.clone());
		}
		
		private function onExpandBtnClick(e:MouseEvent):void 
		{
			if (_btnExpand.currentFrame == 1) {
				_btnExpand.gotoAndStop(2);
			}else {
				_btnExpand.gotoAndStop(1);
			}
		}		
		
		////////////////////////////////////////////////////////////
		//// Set和Get函数
		////////////////////////////////////////////////////////////
		public function get selected():Boolean { return _selected; }
		public function set selected(value:Boolean):void 
		{
			_selected = value;
		}
	}

}